
# 临时生效的Command Line Tools下载地址（需要按时更换，调试完成后不用更换），从这里获取  https://developer.huawei.com/consumer/cn/download/
TOOLS_URL="https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_package_901_9/1b/v3/7aaqsqVPR7KuoorhcW9wOA/commandline-tools-linux-x64-5.0.3.806.zip?HW-CC-KV=V1&HW-CC-Date=20240921T062714Z&HW-CC-Expire=7200&HW-CC-Sign=BA07532F52359F4320918CA6D9A313A6BD4898758B06A8EC8FD031032358C595"

TOOLS_SHA256="49416454982f8e73958e3917ad4c6b9a56b8a59ada4a58aa3c6692701ce774f0"

PRJ_DIR=$(pwd)/third_party/ohos_react_native

WORK_DIR=$(pwd)/RN_Work

# 检查WORK_DIR目录是否被删除
echo "Print the contents of the RN_Work directory:"
ls "$WORK_DIR"/
echo "Printing content completed"

# rm -rf "$WORK_DIR"    # 调试好WORK_DIR后要注释此行
mkdir -p "$WORK_DIR"

TOOLS_DIR="$WORK_DIR"/command-line-tools

PREBUILD_DIR="$WORK_DIR"/rnoh_prebuild

echo "打印 command-line-tools.zip 文件信息"
stat "$WORK_DIR"/command-line-tools.zip
echo "完成打印 command-line-tools.zip 文件信息"

# exit 2  # 调试！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！

#
function prepare_node() {
    echo "start prepare Node.js"

    cd "$TOOLS_DIR"
	if [ ! -d tool ]; then
	    echo "Error: Path '$TOOLS_DIR/tool' not found."
		return 1
	fi
	
	rm -rf tool/*
	
	tar -xJf "$PREBUILD_DIR"/node_17/tool.tar.xz
	if [ $? -ne 0 -a $? -ne 1 ]; then
	    rm -rf tool/*
		echo "Failed to extract tool.tar.xz!!!"
		return 1
	fi
	echo "Unzipped tool.tar.xz successfully!!!"
	
    return 0
}

function prepare_tools() {
    echo "start prepare command-line-tools"
	cd "$WORK_DIR"
	
	# 此块用于测试SHA256
	# echo "Note: TOOLS_SHA256 is:  $TOOLS_SHA256"
	# if [[ $TOOLS_SHA256 =~ ^[0-9a-fA-F]{64}$ ]]; then
	# 	echo "TOOLS_SHA256 is a valid SHA256 value"
	# else
	# 	echo "TOOLS_SHA256 is not a valid SHA256 value"
	# 	return 1
	# fi

	if [ ! -d command-line-tools ]; then
        if [ ! -f ./command-line-tools.zip ]; then
			curl -f -L -- "$TOOLS_URL" > command-line-tools.zip
			if [ $? -ne 0 ]; then
				echo "Error: download command-line-tools.zip failed!!!"
				return 1
			fi
		fi
        echo "The command-line-tools.zip has been downloaded"

        echo "打印 command-line-tools.zip 文件信息"
        stat command-line-tools.zip
        echo "完成打印 command-line-tools.zip 文件信息"
		
		echo "Note: TOOLS_SHA256 is:  $TOOLS_SHA256"
		# 检查 TOOLS_SHA256 是否符合 SHA-256 哈希的格式
		if [[ $TOOLS_SHA256 =~ ^[0-9a-fA-F]{64}$ ]]; then
			echo "TOOLS_SHA256 is a valid SHA256 value"
		else
			echo "TOOLS_SHA256 is not a valid SHA256 value"
			return 1
		fi
		
		local tools_sum=$(sha256sum command-line-tools.zip)
		tools_sum=${tools_sum:0:64}  # SHA-256 产生64个字符的哈希值
		if [ "x$tools_sum" != "x$TOOLS_SHA256" ]; then
			echo "tools_sum(no x) is : x$tools_sum"
			echo "SHA-256_command-line-tools(no x) is : x$TOOLS_SHA256"
			echo "Error: Two hashes are not equal,please check!!!Deleting command-line-tools!!!"
			
			echo "重新下载 command-line-tools.zip ，注意，为提高通过率，暂时不校验SHA-256"
			rm -f command-line-tools.zip
			curl -f -L -- "$TOOLS_URL" > command-line-tools.zip
			if [ $? -ne 0 ]; then
				echo "Error: Retry download command-line-tools.zip failed!!!"
				return 1
			fi
			echo "Retry download command-line-tools.zip successfully!!!暂时不校验SHA-256!!!"
		fi
		echo "The hashes are equal"
		
        echo "Start to unzip command-line-tools.zip"
		unzip -qo command-line-tools.zip
		if [ $? -ne 0 ]; then
			echo "Failed to extract command-line-tools.zip!!!"
			return 1
		fi
		echo "unzip command-line-tools.zip successfully"
		
		# ===================================================================================================================
		
		prepare_node
		if [ $? -ne 0 ]; then
			echo "Failed to prepare Node.js!!!"
			return 1
		fi
		echo "Prepare Node.js successfully"
	fi

	
	echo "The command-line-tools path already exists"

    # sleep 3  # 暂停3秒，用于调试
	
    return 0
}

function prepare_config_files() {
    echo "start prepare config_files"

	cp "$PREBUILD_DIR"/config_files/npm/.npmrc ~/
	if [ $? -ne 0 ]; then
        echo "Failed to copy npmrc!!!"
        return 1
    fi

	rm -rf ~/.ohpm/ && mkdir ~/.ohpm/
    cp "$PREBUILD_DIR"/config_files/ohpm/.ohpmrc ~/.ohpm/
	if [ $? -ne 0 ]; then
        echo "Failed to copy .ohpmrc!!!"
        return 1
    fi
	echo ".npmrc and .ohpmrc already exists"

    # sleep 3  # 暂停3秒，用于调试
	
    return 0
}

function prepare_project() {
    echo "start prepare project"
	
	cd "$PRJ_DIR"
	if [ $? -ne 0 ]; then
		echo "Error: Cannot find ohos_react_native!!!"
		return 1
	fi
	echo "The ohos_react_native path already exists"

    cd ./tester/harmony
	if [ $? -ne 0 ]; then
		echo "Error: Cannot find harmony project!!!"
		return 1
	fi
	echo "The harmony project path already exists"
	
	cp build-profile.template.json5 build-profile.json5
	
	#
	#  下载hvigor插件和生成local.properties文件的事，hvigor会自己处理，不用手动下载
	#

    # sleep 3  # 暂停3秒，用于调试
	
    return 0
}

function prepare_third_party() {
    echo "start prepare third_party"
	
	cd "$PRJ_DIR"/tester/harmony/react_native_openharmony/src/main/cpp/third-party
	if [ $? -ne 0 ]; then
	    echo "Cannot find the third-party of ohos_react_native!!!"
		# sleep 3  # 暂停3秒，用于调试
	    rm -rf "$PRJ_DIR"/
	fi
	echo "The third-party of ohos_react_native already exists"
	
    cp "$PREBUILD_DIR"/third_party/* ./
	if [ $? -ne 0 ]; then
		echo "Failed to copy third-party!!!"
		rm -f boost_part000 boost_part001 fmt.zip folly.zip hermes.zip libevent.zip
		return 1
	fi
	echo "copy third_party successfully"

    cat boost_part* > boost.zip
	rm -f boost_part000 boost_part001
	
	for file in $(find ./ -maxdepth 1 -type f)
	do
		unzip -qo "$file"   # quiet and overwrite
		if [ $? -ne 0 -a $? -ne 1 ]; then
			echo "Failed to extract file $file!!!"
			rm -rf boost* fmt* folly* hermes* libevent*
			return 1
		fi
		rm -f "$file"
		echo "Unzipped $file successfully!!!"
		# sleep 3  # 暂停3秒，用于调试
	done
	echo "Unzipped third-party successfully!!!"
	
	# sleep 3  # 暂停3秒，用于调试
	
    return 0
}

function prepare() {
	cd "$WORK_DIR"
	if [ $? -ne 0 ]; then
	    echo "$WORK_DIR 不存在!!!"
        return 1
	fi
	
	echo "start prepare build"
	
    if [ -d rnoh_prebuild ]; then
	    echo "Start to sync rnoh_prebuild ########"
	    cd rnoh_prebuild
		git fetch origin
		git reset --hard origin/master
		cd ..
	else
	    echo "Start to clone rnoh_prebuild ########"
		git clone https://gitee.com/jarayard/rnoh_prebuild.git --depth 1
		if [ $? -ne 0 ]; then
			echo "Warning: git clone rnoh_prebuild failed! Start retry!!!"
			rm -rf "$PREBUILD_DIR"/
			git clone https://gitee.com/jarayard/rnoh_prebuild.git --depth 1
			if [ $? -ne 0 ]; then
			    rm -rf "$PREBUILD_DIR"/
				echo "Error: git clone rnoh_prebuild failed!!!"
			    return 1
			fi
		fi
	fi
	echo "The rnoh_prebuild path already exists"
	
    # 测试
    # prepare_tools
    # export PATH="$WORK_DIR"/command-line-tools/bin:"$WORK_DIR"/command-line-tools/tool/node/bin:$PATH
    # npm -v
    # hvigorw -v
    # if [ $? -ne 0 ]; then
    #     echo "Node版本错误!!!"
    #     return 1
    # fi
    # echo "Node版本正确!!!"
    # return 1
    # 测试
    
	# ===================================================================================================================
    # ===================================================================================================================

	prepare_project
	if [ $? -ne 0 ]; then
        echo "Failed to execute prepare_project()!!!"
        return 1
    fi
	# ===================================================================================================================
	prepare_third_party
	if [ $? -ne 0 ]; then
        echo "Failed to execute prepare_third_party()!!!"
        return 1
    fi
	# ===================================================================================================================
    prepare_tools
	if [ $? -ne 0 ]; then
	    rm -rf "$TOOLS_DIR"/
        echo "Failed to execute prepare_tools()!!!"
        return 1
    fi
	# ===================================================================================================================
	prepare_config_files
	if [ $? -ne 0 ]; then
        echo "Failed to execute prepare_config_files()!!!"
        return 1
    fi

	# ===================================================================================================================
	# ===================================================================================================================

    echo "prepare build successfully!!!"
	
    return 0
}

prepare $@
exit $?